package com.seele.tigerwallet.ui.activtiy;


import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.seele.base.utils.DeviceUtils;
import com.seele.base.utils.Utils;
import com.seele.tigerwallet.BuildConfig;
import com.seele.tigerwallet.R;
import com.seele.tigerwallet.api.APIException;
import com.seele.tigerwallet.async.Log;
import com.seele.tigerwallet.model.ContactsModel;
import com.seele.tigerwallet.model.UserInfo;
import com.seele.tigerwallet.ui.MainActivity;
import com.seele.tigerwallet.ui.RegisterActivity;
import com.seele.tigerwallet.ui.base.BaseActivity;
import com.seele.tigerwallet.ui.callback.OnLoginResponseListener;
import com.seele.tigerwallet.ui.manager.DaoManager;
import com.seele.tigerwallet.ui.manager.LoginManager;
import com.seele.tigerwallet.ui.utils.BaseDialog;
import com.seele.tigerwallet.ui.utils.BaseUtil;
import com.seele.tigerwallet.ui.utils.LoadWebViewManager;
import com.seele.tigerwallet.ui.utils.SPUtils;
import com.seele.tigerwallet.ui.utils.SqlUtil;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import retrofit2.Call;

/**
 * 闪屏页面
 */
public class WelcomeActivity extends BaseActivity {

    private LoginManager mLoginManager;//用户登录管理

    private BaseDialog mBaseDialog;//对话框

    private TextView mTitleTv;
    private TextView mContentTv;
    private TextView mChildTitleTv;
    private TextView mKnowTv;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    private void initDialog() {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.know_tv:
                        mBaseDialog.cancel();
                        startRegister();
                        break;
                }
            }
        };

        //初始化对话框
        if (null == mBaseDialog) {
            mBaseDialog = new BaseDialog.Builder(this)
                    .view(R.layout.dialog_iscreenshot)
                    .setWidthPX((int) getResources().getDimension(R.dimen.dp_270))
                    .setHeightPX((int) getResources().getDimension(R.dimen.dp_180))
                    .cancelTouchout(false)
                    .addViewOnclick(R.id.know_tv, listener)
                    .setDialogPosition(Gravity.CENTER) //设置dialog显示位置
                    .build();
        }

        mTitleTv = mBaseDialog.findView(R.id.title_tv);
        mContentTv = mBaseDialog.findView(R.id.content_tv);
        mChildTitleTv = mBaseDialog.findView(R.id.child_title_tv);

        mChildTitleTv.setText(getResources().getText(R.string.risk_child_title_hit));
        mChildTitleTv.setVisibility(View.VISIBLE);
        mTitleTv.setText(getResources().getText(R.string.risk_title_hit));
        mContentTv.setText(getResources().getText(R.string.risk_content_hit));
        mBaseDialog.show();


    }

    private void unSafeApkDialog() {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.know_tv:
                        mBaseDialog.cancel();
                        finish();
                        break;
                }
            }
        };

        if (null == mBaseDialog) {
            mBaseDialog = new BaseDialog.Builder(this)
                    .view(R.layout.dialog_iscreenshot)
                    .setWidthPX((int) getResources().getDimension(R.dimen.dp_270))
                    .setHeightPX((int) getResources().getDimension(R.dimen.dp_180))
                    .cancelTouchout(false)
                    .addViewOnclick(R.id.know_tv, listener)
                    .setDialogPosition(Gravity.CENTER) //设置dialog显示位置
                    .build();
        }

        mTitleTv = mBaseDialog.findView(R.id.title_tv);
        mContentTv = mBaseDialog.findView(R.id.content_tv);
        mChildTitleTv = mBaseDialog.findView(R.id.child_title_tv);
        mKnowTv = mBaseDialog.findView(R.id.know_tv);

        mChildTitleTv.setText(getResources().getText(R.string.unsageapk_child_title_hit));
        mChildTitleTv.setVisibility(View.VISIBLE);
        mTitleTv.setText(getResources().getText(R.string.risk_title_hit));
        mContentTv.setText(getResources().getText(R.string.unsageapk_content_hit));
        mKnowTv.setText(getResources().getText(R.string.unsageapk_finish));
        mBaseDialog.show();
    }


    @Override
    protected void init() {
        initWidow();



        if (null == mLoginManager) {
            mLoginManager = new LoginManager();
        }

        mLoginManager.setOnResponseListener(new OnLoginResponseListener() {
            @Override
            public void onResponse(boolean isUpload) {
                start();
            }

            @Override
            public void onFailureError(Call<String> call, Throwable t, int errorCode) {
                start();
            }

            @Override
            public void onResponseError(APIException e) {
                start();
            }

            @Override
            public void onRegisterResponse() {
                if (BuildConfig.CHECK_SAFE && com.seele.tigerwallet.utils.Utils.isUnSafeApk()) {
                    unSafeApkDialog();
                } else
                    if (DeviceUtils.isDeviceRooted()) {
                    initDialog();
                } else {
                    startRegister();
                }
            }
        });
        mLoginManager.reLogin();
    }

    /**
     * 注册
     */
    private void startRegister() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(RegisterActivity.class);
                finish();
            }
        }, 50);
    }


    private void start() {
        if (SPUtils.getBoolean(SPUtils.FINGERPRINTS, false)) {
            startActivity(UnlockingActivity.class);
            finish();
        } else {
            startActivity(MainActivity.class);
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if (null != mLoginManager) {
            mLoginManager = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
